package cn.stylefeng.guns.modular.business.service.impl;


import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.pojo.node.TreeviewNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.config.redis.RedisHandler;
import cn.stylefeng.guns.core.MessageNoticeType;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.common.Constants;
import cn.stylefeng.guns.modular.business.common.GunsData;
import cn.stylefeng.guns.modular.business.entity.Category;
import cn.stylefeng.guns.modular.business.entity.CategoryContent;
import cn.stylefeng.guns.modular.business.entity.MessageNotice;
import cn.stylefeng.guns.modular.business.mapper.CategoryMapper;
import cn.stylefeng.guns.modular.business.model.result.*;
import cn.stylefeng.guns.modular.business.service.CategoryContentService;
import cn.stylefeng.guns.modular.business.service.CategoryService;
import cn.stylefeng.guns.modular.business.service.IndexService;
import cn.stylefeng.guns.modular.business.service.MessageNoticeService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.util.StringUtils;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class IndexServiceImpl extends ServiceImpl<CategoryMapper, Category> implements IndexService {

    @Resource
    private CategoryMapper categoryMapper;

    @Autowired
    RedisHandler redisHandler3;

    @Autowired
    private CategoryContentService categoryContentService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MessageNoticeService messageNoticeService;


    /**
     * 获取首页数据
     *
     * @return
     */
    @Override
    public IndexResult getIndexData() {
        log.info("请求首页数据");
        //判断是否存在首页数据，存在则不请求
        IndexResult indexResult = GunsData.indexMap.get(Constants.INDEX_MAP_KEY);
        if (indexResult == null) {
            indexResult = new IndexResult();
            GunsData.indexMap.put(Constants.INDEX_MAP_KEY, indexResult);
        }
        if (indexResult.getCardList() == null) {
            //获取卡信息
            String gunsIndexCardId = (String) ConstantsContext.getConstntsMap().get("GUNS_INDEX_CARD_ID");
            if (StringUtils.isNotEmpty(gunsIndexCardId)) {
                List<CategoryResultView> cardList = categoryService.getCategories(Long.valueOf(gunsIndexCardId), false);
                indexResult.setCardList(cardList);
            }
        }

        //获取公告
        MessageNoticeResult messageNoticeResult = new MessageNoticeResult();
        if (indexResult.getMessageNoticeResult() == null) {
            Page<MessageNoticeResult> page = new Page<>(1, 1);
            List<MessageNotice> list = messageNoticeService.list(page, null, MessageNoticeType.notice.getCode());
            if (list != null) {
                MessageNotice messageNotice = list.get(0);
                ToolUtil.copyProperties(messageNotice, messageNoticeResult);
                indexResult.setMessageNoticeResult(messageNoticeResult);
            }
        }

        //获取积分类别
        if(indexResult.getIntegralList() == null) {
            String gunsIntegralCardId = (String) ConstantsContext.getConstntsMap().get("GUNS_INDEX_INTEGRAL_ID");
            if (StringUtils.isNotEmpty(gunsIntegralCardId)) {
                List<CategoryResultView> iList = categoryService.getCategories(Long.valueOf(gunsIntegralCardId), false);
                indexResult.setIntegralList(iList);
            }
        }

        //获取热门卡类
        if(indexResult.getHotCardList() == null) {
            String gunsHotCardId = (String) ConstantsContext.getConstntsMap().get("GUNS_INDEX_HOT_ID");
            if (StringUtils.isNotEmpty(gunsHotCardId)) {
                List<Category> hostlist = categoryService.list(new QueryWrapper<Category>().eq("hot", 1));
                if(hostlist != null) {
                    List<CategoryResultView> bList = JSONObject.parseArray(JSONArray.toJSONString(hostlist), CategoryResultView.class);
                    indexResult.setHotCardList(bList);
                }
            }
        }
        return indexResult;
    }

    /**
     * 获取所有的卡类类型列表
     *
     * @return
     */
    @Override
    public List<CategoryResultView> getAllCardList(String categoryId) {
        log.info("获取所有礼品卡分类");
        //获取卡信息
        List<CategoryResultView> cardList = GunsData.cateList.get(categoryId);
        if(cardList == null) {
            cardList = new ArrayList<>();
            GunsData.cateList.put(categoryId, cardList);
        }
        //如果没有内存就初始化
        if (cardList.size() == 0) {
            if (StringUtils.isNotEmpty(categoryId)) {
                List<CategoryResultView> result = categoryService.getCategories(Long.valueOf(categoryId), true);
                cardList.addAll(result);
            }
        }
        return cardList;
    }

    /**
     * 获取分类下的面值
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<CategoryFrontResultView> getAllCardListAndQuota(String categoryId) {
        log.info("获取所有分类和面值");
        //获取卡信息
        List<CategoryFrontResultView> cardList = GunsData.cardAndQuotaList.get(categoryId);
        if(cardList == null) {
            cardList = new ArrayList<>();
            GunsData.cardAndQuotaList.put(categoryId, cardList);
        }
        //如果没有内存就初始化
        if (cardList.size() == 0) {
            if (StringUtils.isNotEmpty(categoryId)) {
                List<CategoryFrontResultView> result = categoryService.getCategoriesWithQuota(Long.valueOf(categoryId));
                cardList.addAll(result);
            }
        }
        return cardList;
    }

}
